#pragma once
#include "base.h"
#include "FillMode.h"
#include "IRenderer.h"
#include "ICamera.h"
#include "Types.h"
#include <vector>

#include "ModelVBO.h"
#include "ShaderGuiColor.h"
#include "ShaderGuiTexture.h"

#define RENDERER_TEXT_XOFFSET 0
#define RENDERER_TEXT_YOFFSET -3
#define PROJECTION_PERSPECTIVE 0
#define PROJECTION_ORTOGONAL 1

class Renderer : public IRenderer
{
public:
	Renderer(void);
	~Renderer(void);

	//IRenderer Interface members
public:
	bool Init(int screenW, int screenH);
	void Cleanup();
	void Clear();
	void Present();
	void DrawRect2D(const vector2 &pos, const vector2 &size, float rotation, const vector2 &rotationOrigin);
	void DrawRect2D(const vector2 &pos, const vector2 &size);
	void DrawRect3D(const vector3 &pos, const vector2 &size, const vector3 &rotation, const vector2 &rotationOrigin);
	void DrawLine(const vector3 &pointA, const vector3 &pointB, float pointSize);
	void BeginUniformLine(float pointSize);
	void DrawUniformLine(const vector3 &pointA, const vector3 &pointB);
	void EndUniformLine();
	int PrintStr2D(const vector2 &pos, const string &text, const color &c);
	int PrintStr3D(const vector3 &pos, const vector3 &rotation, const string &text, const color &c);
	void SetFillMode(const FillMode &fm);
	void SetColor(const color &c);
	void SetColor(const uint &id, const color &c);
	bool SetGradientLength(const uint &length);
	void SetBillboarding(bool enable);
	void SetTexture(Texture *pTexture);
	boost::shared_ptr<ICamera> GetActiveCamera();
	void SetActiveCamera(boost::shared_ptr<ICamera> pCamera);
	uint GetGradientLength();
	vector2 GetScreenSize();
	matrix GetProjectionMatrix();
	SDL_Window *GetScreenSurface();

private:
	bool LoadShaders();
	bool LoadStdModels();
	bool LoadFonts();
	void DrawTextLine(const vector2 &pos, const string &text, const color &c);
	void _drawRect(const vector3 &pos, const vector2 &size, const vector3 &rotation, const vector2 &rotationOrigin,
	const matrix &projection, const matrix &view);

protected:
	//SDL/GL Handles
	void *m_pGLContext;
	SDL_Window *m_pScreen;
	int m_nGLContextMajor;
	int m_nGLContextMinor;
	int m_lineVertPos;

	TTF_Font *m_pDefFont;
	uint m_nScreenWidth;
	uint m_nScreenHeigth;

	//Common render objects
	ModelVBO m_modelScreenQuad;
	ModelVBO m_modelScreenQuadLines;
	
	//Shaders
	ShaderGuiColor m_shaderColor;
	ShaderGuiTexture m_shaderTexture;

	//Render states
	boost::shared_ptr<ICamera> m_pCamera;
	vector<color> m_gradientColors;
	matrix m_matProjection;
	Texture *m_pDrawTexture;
	bool m_bBillboarding;
	FillMode m_fillMode;
};

